package com.brayan.webapp.service.usermgmt;

import javax.ejb.Stateless;
import javax.inject.Inject;

import com.brayan.webapp.model.general.Modification;
import com.brayan.webapp.model.general.geoinfo.GeoInfo;
import com.brayan.webapp.model.realestate.Tenant;
import com.brayan.webapp.repository.usermgmt.TenantRepository;
import com.brayan.webapp.service.general.geoinfo.GeoIPBean;

@Stateless
public class TenantBean {

	@Inject
	private GeoIPBean geoIpService;
	@Inject
	private TenantRepository tenantRepo;

	public Tenant saveTenant(Tenant tenant) {
		Tenant savedTenant = tenantRepo.saveTenant(tenant);
		return savedTenant;
	}

	public void asyncSaveGeoInfo(long tenantId, String ipAddress) {
		GeoInfo geoInfo = geoIpService.getGeoInfoByIp(ipAddress);
		Modification modi = new Modification();
		modi.setGeoInfo(geoInfo);
		
		Tenant tenant = tenantRepo.findById(tenantId);		
		tenant.addModification(modi);
		tenantRepo.saveTenant(tenant);
	}

}
